package ai.people.platform.archives.intimacy.handler.impl;

import ai.people.core.grpc.lib.key.account.TwitterResponse;
import ai.people.netmon.framework.domain.key.account.entity.intimacy.TwitterIntimacy;
import ai.people.netmon.framework.domain.key.account.enums.IntimacyFieldsEnum;
import ai.people.netmon.framework.domain.key.account.enums.IntimacySupportEnum;
import ai.people.netmon.framework.domain.key.account.vo.ExtendVO;
import ai.people.platform.archives.intimacy.IntimacyHandlerFactory;
import ai.people.platform.archives.intimacy.handler.IntimacyHandleService;
import com.alibaba.fastjson.JSONObject;
import com.vesoft.nebula.client.graph.net.SessionWrapper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * @author zhenglin
 * @date 2022/7/28
 */
@Service
public class TwitterIntimacyHandleServiceImpl implements IntimacyHandleService, InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        IntimacyHandlerFactory.registerIntimacyHandler(IntimacySupportEnum.TWITTER.value, this);
    }

    @Override
    public List<ExtendVO.Extend> analysis(List<?> responseList, Map<String, Integer> ruleMapping, String username, SessionWrapper session) {
        List<TwitterResponse> data = (List<TwitterResponse>) responseList;

        List<TwitterIntimacy> twitterIntimacyList = new ArrayList<>();
        for (TwitterResponse response : data) {
            TwitterIntimacy intimacy = JSONObject.parseObject(JSONObject.toJSONString(response), TwitterIntimacy.class);
            intimacy.setUsername(response.getUsername());
            int followWight = getFieldWeight(IntimacyFieldsEnum.follow.name(), ruleMapping);
            int forwardWight = getFieldWeight(IntimacyFieldsEnum.forward_cnt.name(), ruleMapping);
            int likeWight = getFieldWeight(IntimacyFieldsEnum.like_cnt.name(), ruleMapping);
            int commentWight = getFieldWeight(IntimacyFieldsEnum.comment_cnt.name(), ruleMapping);
            int msgAtWight = getFieldWeight(IntimacyFieldsEnum.msg_at_cnt.name(), ruleMapping);

            int qmd = intimacy.getFollow() * followWight + intimacy.getForwardCnt() * forwardWight
                    + intimacy.getLikeCnt() * likeWight + intimacy.getCommentCnt() * commentWight
                    + intimacy.getMsgAtCnt() * msgAtWight;
            intimacy.setQmd(qmd);
            twitterIntimacyList.add(intimacy);
        }
        List<ExtendVO.Extend> res = new ArrayList<>();
        twitterIntimacyList.sort(Comparator.comparingInt(TwitterIntimacy::getQmd).reversed());
        for (TwitterIntimacy intimacy : twitterIntimacyList) {
            ExtendVO.Extend extendVO = new ExtendVO.Extend();
            extendVO.setUsername(intimacy.getUsername());
            extendVO.setQmd(intimacy.getQmd());

            int follow = intimacy.getFollow();
            int forwardCnt = intimacy.getForwardCnt();
            int likeCnt = intimacy.getLikeCnt();
            int commentCnt = intimacy.getCommentCnt();
            int msgAtCnt = intimacy.getMsgAtCnt();

            JSONObject obj = new JSONObject();
            obj.put(IntimacyFieldsEnum.follow.name(), follow > 0);
            obj.put(IntimacyFieldsEnum.forward_cnt.name(), forwardCnt);
            obj.put(IntimacyFieldsEnum.like_cnt.name(), likeCnt);
            obj.put(IntimacyFieldsEnum.comment_cnt.name(), commentCnt);
            obj.put(IntimacyFieldsEnum.msg_at_cnt.name(), msgAtCnt);

            extendVO.setBasis(obj);
            res.add(extendVO);
        }
        return res;
    }

}
